<?php

class Languages extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{languages}}';
	}

	public function rules()
	{
		return array(
			array('code, name', 'required'),
			array('code', 'length', 'max'=>2),
			array('name', 'length', 'max'=>16),
			array('id, code, name', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'code' => tt('Code'),
			'name' => tt('Name'),
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('name',$this->name,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave()
	{
		if($this->isNewRecord)
		{
			$modules = getLanguageModules();
			foreach ($modules as $m)
			{
				Yii::import('application.modules.install.models.*');
				$config = Install::getModuleInstaller($m['name'])->config();
				$tables = $config['dependencies']['languages'];
				foreach ($tables as $table=>$options)
				{
					foreach ($options['fields'] as $name=>$type)
					{
						Yii::app()->db->createCommand()->addColumn($table, $name.'_'.$this->code, $type);
						Yii::app()->db->createCommand('UPDATE '.$table.' SET '.$name.'_'.$this->code.'='.$name.'_'.Settings::model()->get('localization', 'defaultLang').' WHERE 1')->execute();
					}
				}
			}
		}
		return parent::beforeSave();
	}
	
	public function save($runValidation=true,$attributes=null)
	{
		return parent::save($runValidation, $attributes);
	}
	
	public function afterSave()
	{
		$this->updateCache();
		parent::afterSave();
	}
	
	public function afterDelete()
	{
		$modules = getLanguageModules();
		foreach ($modules as $m)
		{
			Yii::import('application.modules.install.models.*');
			$config = Install::getModuleInstaller($m['name'])->config();
			$tables = $config['dependencies']['languages'];
			foreach ($tables as $table=>$options)
			{
				foreach ($options['fields'] as $name=>$type)
				{
					Yii::app()->db->createCommand()->dropColumn($table, $name.'_'.$this->code);
				}
			}
		}
			
		$this->updateCache();
		return parent::afterDelete();
	}
	
	private function selectLanguages($key=null)
	{
		$languages = $this->findAll();
		$result = array();
		foreach ($languages as $l)
		{
			$result['languages'][] = array(
				'code' => $l->code,
				'name' => $l->name
			);
			$result['languageCodes'][] = $l->code;
		}

		return (!empty($key))?$result[$key]:$result;
	}
	
	public function updateCache()
	{
		$languages = $this->selectLanguages();
		
		Yii::app()->cache->set('languages', $languages['languages']);
		Yii::app()->cache->set('languageCodes', $languages['languageCodes']);
	}
	
	public function getLanguages()
	{
		if (!param('cacheEnabled'))
		{
			return $this->selectLanguages('languages');
		}
		
		$languages = Yii::app()->cache->get('languages');
		if (!$languages)
		{
			Languages::model()->updateCache();
			$languages = Yii::app()->cache->get('languages');
		}
		return $languages;
	}
	
	public function getLanguageCodes()
	{
		if (!param('cacheEnabled'))
		{
			return $this->selectLanguages('languageCodes');
		}
		
		$languageCodes = Yii::app()->cache->get('languageCodes');
		if (!$languageCodes)
		{
			Languages::model()->updateCache();
			$languageCodes = Yii::app()->cache->get('languageCodes');
		}

		return $languageCodes;
	}
}